package cn.lyh.gulimall.product.controller;


import cn.lyh.gulimall.product.entity.PmsBrand;
import cn.lyh.gulimall.product.service.PmsBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import utils.PageUtils;
import utils.R;
import valid.AddGroup;
import valid.UpdateGroup;
import valid.UpdateBrandStatusGroup;

import java.util.Arrays;
import java.util.Map;

/**
 * @author lyh
 * @version 1.0
 * @since 2021/4/9
 */
@RequestMapping("/product/brand")
@RestController
public class PmsBrandController {


	@Autowired
	PmsBrandService brandService;


	/**
	 * 品牌列表分页查询
	 *
	 * @param params 前端传入的查询参数包括 pageIndex，pageSize key为page，limit
	 * @return 查询结果
	 */
	@GetMapping("/list")
	public R getList(@RequestParam Map<String, Object> params) {
		PageUtils pageUtils = brandService.queryPage(params);
		return R.ok().put("page", pageUtils);
	}

	/**
	 * 商品品牌保存方法
	 *
	 * @param pmsBrand Brand实体类
	 * @return 操作结果
	 */
	@PostMapping("/save")
	public R save(@Validated(AddGroup.class) @RequestBody PmsBrand pmsBrand) {
		brandService.save(pmsBrand);
		return R.ok();
	}

	/**
	 * 批量删除方法
	 *
	 * @param brandIds ID数组
	 * @return 操作结果
	 */
	@PostMapping("/delete")
	public R delete(@RequestBody Long[] brandIds) {
		brandService.removeByIds(Arrays.asList(brandIds));
		return R.ok();
	}

	/**
	 * 数据修改方法
	 *
	 * @param pmsBrand Brand数据实体类
	 * @return 操作结果
	 */
	@PostMapping("/update")
	public R update(@Validated(UpdateGroup.class) @RequestBody PmsBrand pmsBrand) {
		brandService.updateDetail(pmsBrand);
		return R.ok();
	}

	/**
	 * 根据ID查询品牌信息
	 *
	 * @param brandId id
	 * @return 操作结果
	 */
	@GetMapping("/info/{brandId}")
	public R info(@PathVariable("brandId") Long brandId) {
		PmsBrand brand = brandService.getById(brandId);
		return R.ok().put("brand", brand);
	}

	/**
	 * 修改状态
	 *
	 * @param brand brand实体类
	 * @return 操作结果
	 */
	@RequestMapping("/update/status")
	public R updateStatus(@Validated(UpdateBrandStatusGroup.class) @RequestBody PmsBrand brand) {
		brandService.updateById(brand);
		return R.ok();
	}


}